package com.gqh.weather.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.gqh.weather.auth.UserContext;
import com.gqh.weather.auth.UserInfo;
import com.gqh.weather.domain.dto.CcSchoolListRequest;
import com.gqh.weather.domain.dto.CcSchoolListResponse;
import com.gqh.weather.domain.dto.CcSchoolLockRequest;
import com.gqh.weather.domain.dto.CcSchoolUpdateRequest;
import com.gqh.weather.domain.dto.CcSchoolUpdateResponse;
import com.gqh.weather.exception.Result;
import com.gqh.weather.service.CCSchoolService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;


/**
 * 抽测学校管理控制器
 */
@Slf4j
//@RestController
@RequestMapping("/pingjia/zh/v1/task/cc_school")
@Validated
public class CCSchoolController {

    @Autowired
    private CCSchoolService ccSchoolService;

    /**
     * 抽测学校列表查询
     *
     * @param page        页数
     * @param count       每页数量,默认20
     * @param citycode    城市码
     * @param taskId      任务id
     * @param matchState  匹配状态 0全部 1匹配 2不匹配
     * @param keyword     搜索关键字
     * @return 抽测学校列表
     */
    @GetMapping("/list")
    public Result<CcSchoolListResponse> getCcSchoolList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer count,
            @RequestParam(required = false) String citycode,
            @RequestParam(name = "task_id") @NotNull(message = "任务id不能为空") Integer taskId,
            @RequestParam(name = "match_state", required=false) Integer matchState,
            @RequestParam(required = false) String keyword) {

        log.info("查询抽测学校列表，任务ID: {}, 匹配状态: {}, 关键字: {}", taskId, matchState, keyword);

        // 参数验证
        if (taskId == null || taskId <= 0) {
            return Result.error("任务id参数不正确");
        }
        
        // 获取用户信息
        UserInfo userInfo = UserContext.getCurrentUser();
        if (citycode == null || citycode.isEmpty()) {
            citycode = userInfo.getCitycode();
        }

        return ccSchoolService.getCcSchoolList(citycode, taskId, matchState, page, count, keyword);
    }

    /**
     * 抽测学校修改
     *
     * @param taskId   任务id
     * @param schoolId 学校id
     * @param count    抽测人数
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result<CcSchoolUpdateResponse> updateCcSchool(
            @RequestParam(name = "task_id") @NotNull(message = "任务ID不能为空") Integer taskId,
            @RequestParam(name = "school_id") @NotNull(message = "学校ID不能为空") Integer schoolId,
            @RequestParam @NotNull(message = "抽测人数不能为空") Integer count) {

        log.info("修改抽测学校，任务ID: {}, 学校ID: {}, 抽测人数: {}", taskId, schoolId, count);

        if (taskId == null || taskId <= 0) {
            return Result.error("任务ID参数不正确");
        }

        if (schoolId == null || schoolId <= 0) {
            return Result.error("学校ID参数不正确");
        }
        
        if (count == null || count < 0) {
            return Result.error("抽测人数参数不正确");
        }

        return ccSchoolService.updateCcSchool(taskId, schoolId, count);
    }

    /**
     * 抽测学校锁定
     *
     * @param taskId 任务id
     * @return 锁定结果
     */
    @PostMapping("/lock")
    public Result<Void> lockCcSchool(
            @RequestParam(name = "task_id") @NotNull(message = "任务ID不能为空") Integer taskId) {

        log.info("锁定抽测学校，任务ID: {}", taskId);

        if (taskId == null || taskId <= 0) {
            return Result.error("任务ID参数不正确");
        }

        return ccSchoolService.lockCcSchool(taskId);
    }
}